package com.ll.homework;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author 刘龙
 * @version 1.0
 */
public class Homework02 {
    public static void main(String[] args) {
        ArrayList<Car> arrayList = new ArrayList<>();

        Car car1 = new Car("宝马", 400000);
        Car car2 = new Car("宾利", 5000000);

        // 添加元素
        arrayList.add(car1);
        arrayList.add(car2);

        // 删除元素
        arrayList.remove(car1);

        // 查找指定元素是否存在
        // false
        System.out.println(arrayList.contains(car1));

        // 清空元素
        // arrayList.clear();

        // 添加多个元素
        arrayList.addAll(arrayList);

        //判断多个元素是否存在
        // true
        System.out.println(arrayList.containsAll(arrayList));

        // 删除多个元素, 这里相当于清空
        // arrayList.removeAll(arrayList);

        // 遍历
        for (Car car : arrayList) {
            System.out.println(car);
        }

        //迭代器
        Iterator<Car> iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            Car car = iterator.next();
            System.out.println(car);
        }
    }
}

class Car {
    private String name;
    private double price;

    public Car(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}
